package com.example.sunsg.okhttptest.bean;

/**
 * Created by sunsg on 15/10/28.
 */

import android.util.Log;

import com.example.sunsg.okhttptest.LongImageWebViewActivity;
import com.example.sunsg.okhttptest.util.ImageUtil;
import com.example.sunsg.okhttptest.util.StorageUtils;

import java.io.Serializable;

/**
 * 压缩图片的任务
 */
public class CompressImageTask implements Runnable, Serializable {
    private static final long serialVersionUID = 0;
    private static int consumeTaskSleepTime = 2000;
    // 保存任务所需要的数据
    private Object threadPoolTaskData;
    private CallBack mCallBack;

    public CompressImageTask(Object tasks, CallBack callback) {
        this.threadPoolTaskData = tasks;
        this.mCallBack = callback;
    }

    public void run() {
        // 处理一个任务，这里的处理方式太简单了，仅仅是一个打印语句
        Log.i("test", Thread.currentThread().getName());
        Log.i("test", "start .." + threadPoolTaskData);
//        System.out.println(Thread.currentThread().getName());
//        System.out.println("start .." + threadPoolTaskData);

//        try {
//            // //便于观察，等待一段时间
//            //相当于执行一个耗时的任务
//            Thread.sleep(consumeTaskSleepTime);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        LongImageWebViewActivity.Item item = null;
        String target;
        try {
            if (threadPoolTaskData instanceof LongImageWebViewActivity.Item) {
                boolean isCompless = false;
                item = (LongImageWebViewActivity.Item) threadPoolTaskData;
                target = StorageUtils.mkdir() + "/path_" + System.currentTimeMillis() + "_.jpg";
//                        target = "ame";
                isCompless = ImageUtil.commonCompressPic(item.sourcePath, target, 1600, 1600);
                if (isCompless) {
                    Log.i("test", "complsesss isSuccess path = " + item.sourcePath);
                    item.compressPath = target;
                } else {
                    Log.i("test", "complsesss not isSuccess  path = " + item.sourcePath);
                }
            }

        } catch (Exception e) {

        }
        mCallBack.call(item);

    }

    public Object getTask() {
        return this.threadPoolTaskData;
    }
}
